package com.homedo.microservice.open.service.gateway.service;

import com.google.common.collect.Lists;
import com.homedo.common.bean.vo.context.UserContext;
import com.homedo.common.dao.bean.po.base.BasePo;
import com.homedo.common.service.BaseService;
import com.homedo.common.util.Detect;
import com.homedo.microservice.open.service.gateway.bean.po.Config;
import com.homedo.microservice.open.service.gateway.bean.po.enhanced.ConfigEnhanced;
import com.homedo.microservice.open.service.gateway.persistence.dao.ConfigDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 作者 jgshun
 * @date 创建时间 2018年09月11日 17时25分30秒
 */
@Service
public class ConfigService extends BaseService<Config> {
    @Autowired
    private ConfigDao configDao;


    @Override
    public List<ConfigEnhanced> getEnhanceds(List<?> ids, UserContext userContext) {
        List<Config> configs = configDao.getListByIds(ids, Config.class);

        if (!Detect.notEmpty(configs)) {
            return null;
        }

        return this.convent2Enhanceds(configs);
    }


    @Override
    public List<ConfigEnhanced> getEnhanceds4Active(List<?> ids, UserContext userContext) {
        List<Config> configs = configDao.getListByIdsGreaterThanMark0(ids, Config.class);

        if (!Detect.notEmpty(configs)) {
            return null;
        }

        return this.convent2Enhanceds(configs);
    }


    @Override
    public List<ConfigEnhanced> convent2Enhanceds(List<? extends BasePo> pos) {
        List<Config> configs = (List<Config>) pos;
        List<ConfigEnhanced> configEnhanceds = Lists.newArrayList();
        configs.forEach(e -> configEnhanceds.add(new ConfigEnhanced(e)));
        return configEnhanceds;
    }


    public List<ConfigEnhanced> getEnhanceds4Application(String applicationName, List<String> disablePropertyNames, UserContext userContext) {
        List<Config> configs = configDao.getListByApplication(applicationName, disablePropertyNames);
        if (!Detect.notEmpty(configs)) {
            return null;
        }
        return this.convent2Enhanceds(configs);
    }
}
